package cz.muni.fi.pa165.rentalofconstructionmachinery.service.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.CustomerDAOLocal;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.CustomerDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.CustomerDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.CustomerServiceLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author stefan.rakonjac
 */
@Stateless
public class CustomerServiceImpl implements CustomerServiceLocal {
    
    @EJB
    private CustomerDAOLocal customerDAO;
    
    public void setCustomerDAO(CustomerDAOLocal customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    public void addCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.createCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    public void removeCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.deleteCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    public void modifyCustomer(CustomerDTO customer) {
        if (null == customer) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        customerDAO.updateCustomer(CustomerDTOConverter.dtoToEntity(customer));
    }

    @Override
    public List<CustomerDTO> getAllCustomers() {
        return CustomerDTOConverter.entityToDTOList(customerDAO.getAllCustomers());
    }

    @Override
    public CustomerDTO getCustomerById(Long id) {
        if (null == id) {
            throw new IllegalArgumentException("'id' must not be null!");
        }
        return CustomerDTOConverter.entityToDTO(customerDAO.getCustomerById(id));
    }

    @Override
    public List<CustomerDTO> searchByName(String query) {
        if (null == query) {
            throw new IllegalArgumentException("'query' must not be null!");
        }
        return CustomerDTOConverter.entityToDTOList(customerDAO.searchByName(query));
    }
    
}
